import java.net.InetAddress;
import java.net.UnknownHostException;

/*Class used to organize data received by server*/

public class ReceivedData {

	public String name;
	public int port;
	public InetAddress ip; 
	
	public int key;
	public InetAddress originalIP; //ip of client
	public int originalPort; //port of client
	public String originalName;
	public String todo;
	public String value; //only for insert
	
	public ReceivedData(String s, InetAddress ip, int port) {
		
		this.ip = ip;
		this.port = port;
		this.name = ip.getHostAddress();
		
		String[] arr = s.split("\\s");
		
		//check command
		
		//syntax is COMMAND KEY (INSERT)
		if(arr[0].equals("GET")) {
			this.key = Integer.parseInt(arr[1]);
			this.originalIP = this.ip;
			this.originalPort = this.port;
			this.todo = "GET";
			this.originalName = this.name;
		}
		else if(arr[0].equals("INSERT")) {
			this.key = Integer.parseInt(arr[1]);
			this.originalIP = this.ip;
			this.originalPort = this.port;
			this.todo = "INSERT";
			this.originalName = this.name;
			this.value = arr[2];
		}
		else if(arr[0].equals("DELETE")) {
			this.key = Integer.parseInt(arr[1]);
			this.originalIP = this.ip;
			this.originalPort = this.port;
			this.todo = "DELETE";
			this.originalName = this.name;
		}
		else if(arr[0].equals("FORWARD")) { //syntz is FORWARD COMMAND KEY IP PORT (INSERT)
			System.out.println(s);			
			this.todo = arr[1];
			this.key = Integer.parseInt(arr[2]);
			try {
				this.originalIP = InetAddress.getByName(arr[3]);
			} catch (UnknownHostException e) {
				e.printStackTrace();
			}
			this.originalPort = Integer.parseInt(arr[4]);
			this.originalName = arr[3];
			
			if(this.todo.equals("INSERT")) {
				this.value = arr[5];
			}
		}
	}
}
